#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.db import models
from clariadmin.models import Supplier, HostType, Host
from clariadmin.models import AdditionnalField, ParamAdditionnalField
from common.models import ClientField, Client

INSTALL_STATUS = (
    (1, "Plannifié", "#ffaeae"),
    (2, "En cours", "#aeaeff"),
    (3, "Terminé", "#aeffae"))


class InstallationOrder(models.Model):

    class Meta:
        permissions = (
            ("can_access_host_install", u"Accès installation des hôtes"),
        )

    serial = models.CharField("Numéreau de série", null=True, blank=True, unique=True, default=None, max_length=64)
    hostname = models.CharField("nom d'hote", max_length=128, null=True, blank=True)
    site = ClientField(Client, verbose_name="Client", null=True, blank=True,
                       limit_choices_to={'parent__isnull': False})
    type = models.ForeignKey(HostType, verbose_name="Type d'installation", blank=True, null=True)
    fai_classes = models.CharField("Classes FAI", max_length=256, blank=True, null=True)
    ip = models.CharField("Adresse IP", max_length=64, blank=True, null=True)
    netmask = models.CharField("Netmask", default="255.255.255.0", max_length=64, blank=True, null=True)
    gateway = models.CharField("Gateway", max_length=64, blank=True, null=True)
    dns1 = models.CharField("ip du serveur DNS 1", max_length=64, blank=True, null=True)
    dns2 = models.CharField("ip du serveur DNS 2", max_length=64, null=True, blank=True)
    ip_mca = models.CharField("ip du serveur MCA", max_length=64, null=True, blank=True)
    ip_clarilab = models.CharField("ip du serveur Clarilab", max_length=64, null=True, blank=True)
    supplier = models.ForeignKey(Supplier, verbose_name="Fournisseur", blank=True, null=True)
    inventory = models.CharField("N° d'inventaire", max_length=64, null=True, blank=True)
    commentaire = models.TextField("commentaire", max_length=4096, null=True, blank=True)
    location = models.CharField("emplacement", max_length=128, null=True, blank=True)
    status = models.IntegerField("Statut d'installation", choices=((k, v) for k, v, c in INSTALL_STATUS), default=1)
    rootpw = models.CharField("mot de passe", max_length=64)
    install_secret = models.CharField("mot de passe", max_length=64)
    host = models.OneToOneField(Host, verbose_name="hote créé.", null=True, blank=True)

    def start_install(self):
        self.status = 2
        self.save()

    def complete_install(self, model=None, **kwargs):
        if self.host == None:
            self.create_related_host(model)
        if kwargs and self.type:
            avail_fields = list(ParamAdditionnalField.object.filter(host_type=self.type)
                                                            .filter(api_key__in=kwargs.keys()))
            #ToDo: validate kwargs values
            for item in avail_fields:
                AdditionnalField(host=self.host, field=item, value=kwargs[item.api_key])
        self.status = 3
        self.save()

    def create_related_host(self, model=None):
        h = Host(site=(self.site or None),
                type=self.type,
                os=self.os,
                hostname=self.hostname,
                rootpw=self.rootpw,
                commentaire=self.commentaire,
                ip=self.ip,
                supplier=self.supplier,
                model=model,
                location=self.location,
                serial=self.serial,
                inventory=self.inventory,
                status=None)
        h = h.save()
        self.host = h
        self.save()

    @property
    def status_text(self):
        return dict((k, v) for k, v, c in INSTALL_STATUS)[self.status]

    @property
    def status_color(self):
        return dict((k, c) for k, v, c in INSTALL_STATUS)[self.status]
